<?php
class Controller_Services_EbayMessage extends Controller_Abstract
{
	function actionList()
	{
		$this->_view['title'] = '邮件列表';
		$ps = isset($_GET['ps']) ? $_GET['ps'] : '20';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : '1';
		$this->_view['shop'] = Shop_User_Mapping::getShopByPlatform();
		$condition = Helper_BSS_Normal::buildCondition();
		$condition['shop_user_mapping'] = '1';
		$result = Services_Ebay_Message::search($condition, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			foreach ($result['data'] as &$d)
			{
				Services_Ebay_Message_Extend::getMessageExtend($d);
			}
			unset($d);
			$page = new Helper_BSS_Page($result['pagination'], url('Services::EbayMessage/List', $condition));
			$this->_view['page'] = $page->getPage();
			$this->_view['message'] = $result['data'];
		}
	}
	
	function actionGetMessage()
	{
		$this->_view['title'] = '更新邮件';
		$this->_view['shop'] = Shop_User_Mapping::getShopByPlatform();
		if ($this->_context->isPOST())
		{
			if (isset($_POST['shop_ids']))
			{
				$shop_ids = $_POST['shop_ids'];
				$mapping = Shop_User_Mapping::checkMapping($shop_ids);
				if ($mapping['ack'] == SUCCESS)
				{
					$start_time = isset($_POST['begin_receive_time']) ? date('c', strtotime($_POST['begin_receive_time'])) : date('c', CURRENT_TIMESTAMP);
					$end_time = isset($_POST['end_receive_time']) ? date('c', strtotime($_POST['end_receive_time'] . ' 23:59:59')) : date('c', CURRENT_TIMESTAMP);
					$result = Services_Ebay_Message::getListByGetMyMessage($shop_ids, $start_time, $end_time, $_POST['folder_id']);
					if ($result['ack'] == SUCCESS)
					{
						return $this->_redirectMessage('更新成功', '正在返回列表..', url('Services::EbayMessage/List'), $result['ack']);
					}
					else
					{
						return $this->_redirectMessage('更新失败', $result['message'] . '，正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
					}
				}
				else
				{
					return $this->_redirectMessage('更新失败', $mapping['message'], $_SERVER['HTTP_REFERER'], $mapping['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('更新失败', '请选择店铺，正在返回..', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionReviseFlag()
	{
		$shit->_view['title'] = '修改标志';
		if (isset($_GET['ids']) && isset($_GET['flag']) && isset($_GET['type']))
		{
			$message = Services_Ebay_Message::find('id in (?)', explode(',', $_GET['ids']))->asArray()->getAll();
			$shop_id = Helper_BSS_Array::getDistinctValueToArrayByKey($message, 'shop_id');
			$mapping = Shop_User_Mapping::checkMapping($shop_id);
			if ($mapping['ack'] == SUCCESS)
			{
				foreach ($message as $m)
				{
					$result = Services_Ebay_Message::ReviseMyMessage($m, $_GET['flag'], $_GET['type']);
				}
			}
			else
			{
				return $this->_redirectMessage('标志失败', $mapping['message'], $_SERVER['HTTP_REFERER'], $mapping['ack']);
			}
			return $this->_redirectMessage('标志成功', '正在返回..', $_SERVER['HTTP_REFERER'], SUCCESS);
		}
	}
	
	function actionDetail()
	{
		$this->_view['title'] = '邮件详细';
		if (isset($_GET['id']))
		{
			$message = Services_Ebay_Message::find('id=?', $_GET['id'])->asArray()->getOne();
			if ($message)
			{
				$mapping = Shop_User_Mapping::checkMapping($message['shop_id']);
				if ($mapping['ack'] == SUCCESS)
				{
					if ($message['read'] != Q::ini('custom_flag/services_ebay_message_read/read/value'))
					{
						Services_Ebay_Message::ReviseMyMessage($message, 1, 'read');
					}
					$message['picture'] = Services_Ebay_Picture::find('relate_table_id=? and type=?', $message['id'], Q::ini('custom_flag/services_ebay_picture_type/message_receive/value'))->asArray()->getAll();
					Services_Ebay_Message_Extend::getMessageExtend($message);
					$this->_view['message'] = $message;
					$listing_item = Online_Ebay_Listing_Item::find('online_ebay_listing.item_id=?', $message['shop_item_id'])
						->joinLeft('online_ebay_listing', '', 'online_ebay_listing.id=online_ebay_listing_item.online_ebay_listing_id')
						->joinLeft('currency', 'symbol as currency_symbol', 'currency.id=online_ebay_listing.currency_id')
						->asArray()->getAll();
					Helper_BSS_Normal::getProductImgUrl($listing_item, 'sku');
					$this->_view['listing_item'] = $listing_item;
					$order = Sales_Order_customer_info::find('sales_order_customer_info.shop_buyer_id=? and sales_order.trash_status=?', $message['sender'], Q::ini('custom_flag/sales_order_trash_status/normal/value'))
						->joinLeft('sales_order', 'sales_order.*', 'sales_order.id=sales_order_customer_info.sales_order_id')
						->joinLeft('currency', 'symbol', 'currency.id=sales_order.currency_id')
						->order('sales_order.sales_time desc')->asArray()->getAll();
					$this->_view['order'] = array_slice($order, 0, 5);
					$message_template = Services_Message_Template::find('create_user_id=?', CURRENT_USER_ID)->setColumns('id, name')->asArray()->getAll();
					$this->_view['message_template'] = $message_template;
					$send = Services_Ebay_Message_Send::find('services_ebay_message_id=?', $message['id'])
						->joinLeft('user', 'name as send_user_name', 'user.id=services_ebay_message_send.send_user_id')
						->order('send_time DESC')->asArray()->getAll();
					foreach($send as &$s)
					{
						$s['picture'] = Services_Ebay_Picture::find('relate_table_id=? and type=?', $s['id'], Q::ini('custom_flag/services_ebay_picture_type/message_send/value'))->asArray()->getAll();
					}
					unset($s);
					$this->_view['send'] = $send;
					$send_history = Services_Ebay_Message_Send::find('shop_id=? and shop_item_id=? and recipient=? and services_ebay_message_id!=?', $message['shop_id'], $message['shop_item_id'], $message['sender'], $message['id'])
						->joinLeft('user', 'name as send_user_name', 'user.id=services_ebay_message_send.send_user_id')
						->order('send_time desc')->asArray()->getAll();
					foreach ($send_history as &$sh)
					{
						$sh['picture'] = Services_Ebay_Picture::find('relate_table_id=? and type=?', $sh['id'], Q::ini('custom_flag/services_ebay_picture_type/message_send/value'))->asArray()->getAll();
					}
					unset($sh);
					$this->_view['send_history'] = $send_history;
					$feedback = Services_Ebay_Feedback::find('shop_id=? and user=?', $message['shop_id'], $message['sender'])->order('create_time desc')->limit(0, 5)->asArray()->getAll();
					$this->_view['feedback'] = $feedback;
				}
				else
				{
					return $this->_redirectMessage('操作失败', $mapping['message'], url('Services::EbayMessage/List'), $mapping['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('操作失败', '找不到邮件信息，正在返回...', url('Services::EbayMessage/List'), FAILURE);
			}
		}
	}
	
	function actionDetailBody()
	{
		$this->_view['title'] = '邮件body详细';
		if (isset($_GET['id']))
		{
			$message = Services_Ebay_Message::find('id=?', $_GET['id'])->asArray()->getOne();
			if ($message)
			{
				$mapping = Shop_User_Mapping::checkMapping($message['shop_id']);
				if ($mapping['ack'] == SUCCESS)
				{
					$message_body = Services_Ebay_Message_Extend::find('services_ebay_message_id=?', $_GET['id'])->asArray()->getOne();
					if (! $message_body['body'])
					{
						$request_result = Services_Ebay_Message::GetBodyByGetMyMessage($message);
						if ($request_result['ack'] == SUCCESS)
						{
							$message_body['body'] = $request_result['text'];
						}
					}
					echo($message_body['body']);exit;
				}
				else
				{
					echo('操作失败');exit;
				}
			}
			else
			{
				echo('找不到邮件信息');exit;
			}
		}
	}
	
	function actionMessageTemplateAjax()
	{
		$this->_view['title'] = '邮件模板内容';
		$message_template = Services_Message_Template::find('id=?', $_REQUEST['id'])->asArray()->getOne();
		echo $message_template['body'];
		exit;
	}
	
	function actionSendMessage()
	{
		$this->_view['title'] = '发送邮件';
		if (! empty($_POST['message_id']))
		{
			$message = Services_Ebay_Message::find('id=?', $_POST['message_id'])->asArray()->getOne();
			$mapping = Shop_User_Mapping::checkMapping($message['shop_id']);
			if ($mapping['ack'] == SUCCESS)
			{
				$picture = array();
				if (isset($_FILES) && $_FILES)
				{
					$result = Services_Ebay_Picture::uploadPicture($_FILES, $message['shop_id']);
					if ($result['ack'] == FAILURE)
					{
						return $this->_redirectMessage('操作失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
					}
					$picture = $result['data'];
				}
				$send = array(
					'services_ebay_message_id' => $message['id'],
					'shop_id' => $message['shop_id'],
					'shop_item_id' => $message['shop_item_id'],
					'sender' => $message['recipient'],
					'recipient' => $message['sender'],
					'send_flag' => Q::ini('custom_flag/services_ebay_message_send_send_flag/failure/value'),
					'body' => $_POST['body']
				);
				Services_Ebay_Message_send::create($send);
				$send_id = mysql_insert_id();
				if (isset($_FILES) && $_FILES && isset($picture) && $picture)
				{
					Services_Ebay_Picture::create($send_id, Q::ini('custom_flag/services_ebay_picture_type/message_send/value'), $picture);
				}
				$result = Services_Ebay_Message_Send::AddMemberMessageRTQ($message, htmlspecialchars($_POST['body']), $picture, $send_id);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('发送成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('发送失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('操作失败', $mapping['message'], url('Services::EbayMessage/List'), $mapping['ack']);
			}
		}
	}
	
	function actionResendMessage()
	{
		$this->_view['title'] = '重新发送邮件';
		if (! empty($_POST['send_id']))
		{
			$send = Services_Ebay_Message_Send::find('id=?', $_POST['send_id'])->asArray()->getOne();
			$message = Services_Ebay_Message::find('id=?', $send['services_ebay_message_id'])->asArray()->getOne();
			$picture = Services_Ebay_Picture::find('relate_table_id=? and type=?', $send['id'], Q::ini('custom_flag/services_ebay_picture_type/message_send/value'))->asArray()->getAll();
			$mapping = Shop_User_Mapping::checkMapping($message['shop_id']);
			if ($mapping['ack'] == SUCCESS)
			{
				$result = Services_Ebay_Message_Send::AddMemberMessageRTQ($message, $send['body'], $picture, $send['id']);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('发送成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('发送失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('操作失败', $mapping['message'], url('Services::EbayMessage/List'), $mapping['ack']);
			}
		}
	}
	
	function actionSendList()
	{
		$this->_view['title'] = '发送列表';
		$ps = isset($_GET['ps']) ? $_GET['ps'] : '20';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : '1';
		$this->_view['shop'] = Shop_User_Mapping::getShopByPlatform();
		$condition = Helper_BSS_Normal::buildCondition();
		$condition['shop_user_mapping'] = '1';
		$result = Services_Ebay_Message_Send::search($condition, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			foreach ($result['data'] as &$d)
			{
				Services_Ebay_Message_Extend::getMessageExtend($d);
			}
			unset($d);
			$page = new Helper_BSS_Page($result['pagination'], url('Services::EbayMessage/SendList', $condition));
			$this->_view['page'] = $page->getPage();
			$this->_view['send'] = $result['data'];
		}
	}
	
	function actionSendDetail()
	{
		$this->_view['title'] = '发送详细';
		if (isset($_GET['id']) && $_GET['id'])
		{
			$send = Services_Ebay_Message_Send::find('services_ebay_message_send.id=?', $_GET['id'])
				->joinLeft('user', 'name as send_user_name', 'user.id=services_ebay_message_send.send_user_id')
				->asArray()->getOne();
			$send['picture'] = Services_Ebay_Picture::find('relate_table_id=? and type=?', $send['id'], Q::ini('custom_flag/services_ebay_picture_type/message_send/value'))->asArray()->getAll();
			$this->_view['send'] = $send;
		}
	}
	
	function actionContactMember()
	{
		$this->_view['title'] = '联系顾客';
		$shop_item_id = isset($_GET['shop_item_id']) ? $_GET['shop_item_id'] : '';
		$user = isset($_GET['user']) ? $_GET['user'] : '';
		if ($shop_item_id && $user)
		{
			$listings = Online_Ebay_Listing::find('item_id=?', $shop_item_id)
				->joinLeft('shop', 'shop.name as shop_name', 'shop.id=online_ebay_listing.shop_id')
				->asArray()->getAll();
			Online_Ebay_Listing_Picture::getListingsGalleryPicture($listings);
			$mapping = Shop_User_Mapping::checkMapping($listings[0]['shop_id']);
			if ($mapping['ack'] == SUCCESS)
			{
				$message_template = Services_Message_Template::find('create_user_id=?', CURRENT_USER_ID)->setColumns('id, name')->asArray()->getAll();
				$this->_view['listing'] = $listings[0];
				$this->_view['user'] = $user;
				$this->_view['message_template'] = $message_template;
			}
			else
			{
				return $this->_redirectMessage('操作失败', $mapping['message'], $_SERVER['HTTP_REFERER'], $mapping['ack']);
			}
		}
	}
	
	function actionContactMemberSave()
	{
		$this->_view['title'] = '联系顾客（保存）';
		if ( ! empty($_POST['listing_id']) && ! empty($_POST['user']) && ! empty($_POST['body']))
		{
			$listing = Online_Ebay_Listing::find('online_ebay_listing.id=?', $_POST['listing_id'])
				->joinLeft('shop', 'shop.name as shop_name', 'shop.id=online_ebay_listing.shop_id')
				->asArray()->getOne();
			$mapping = Shop_User_Mapping::checkMapping($listing['shop_id']);
			if ($mapping['ack'] == SUCCESS)
			{
				$picture = array();
				if (isset($_FILES) && $_FILES)
				{
					$result = Services_Ebay_Picture::uploadPicture($_FILES, $listing['shop_id']);
					if ($result['ack'] == FAILURE)
					{
						return $this->_redirectMessage('操作失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
					}
					$picture = $result['data'];
				}
				$send = array(
					'services_ebay_message_id' => 0,
					'shop_id' => $listing['shop_id'],
					'shop_item_id' => $listing['item_id'],
					'sender' => $listing['shop_name'],
					'recipient' => $_POST['user'],
					'send_flag' => Q::ini('custom_flag/services_ebay_message_send_send_flag/failure/value'),
					'body' => $_POST['body']
				);
				Services_Ebay_Message_Send::create($send);
				$send_id = mysql_insert_id();
				if (isset($_FILES) && $_FILES && isset($picture) && $picture)
				{
					Services_Ebay_Picture::create($send_id, Q::ini('custom_flag/services_ebay_picture_type/message_send/value'), $picture);
				}
				$message = array(
					'shop_id' => $listing['shop_id'],
					'shop_item_id' => $listing['item_id'],
					'recipient' => $_POST['user'],
					'body' => $_POST['body'],
					'picture' => $picture
				);
				$result = Services_Ebay_Message_Send::addMemberMessageAAQToPartner($message, $send_id);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('发送成功', '正在返回..', url('Services::EbayMessage/SendList'), $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('发送失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
			else
			{
				return $this->_reidirectMessage('操作失败', $mapping['message'], $_SERVER['HTTP_REFERER'], $mapping['ack']);
			}
		}
		else
		{
			return $this->_redirectMessage('操作失败', '提交信息不能为空', $_SERVER['HTTP_REFERER'], $result['ack']);
		}
	}
}